<?php
namespace app\admin\controller;

use app\admin\my\MyPage;
use app\admin\my\MyConfig;


class We extends Base {
   
	private $we_type = [ '订阅号', '认证订阅号', '服务号','认证服务号' ];
    private $sex = ['未知','男','女'];
    private $qrcode_type=['临时二维码','数值型永久二维码','字符型永久二维码'];

    public function we() {
		$my = new MyPage();
	   
	    $my ->title("微信公众号管理")
		    ->suggest("微信公众号列表")
		    ->buttonNew("weEdit")
		    ->setDb('we')
		    ->keyId()
		    ->keyText('title', '名称')
		    ->keyText('description', '简介')
		    ->keyMap('we_type', '类型', $this->we_type)
		    ->actionEdit()
		    ->actionStatus('delete,disable')
		    ->map('title', '名称', '微信公众号昵称')
		    ->map('wechat_type', '类型', '微信公众号类型', 'select', $this->we_type)
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		return myData($my,'We');
	    } else {
		    return $my->fetch();
	    }
    }



    public function weEdit($id=0) {
		

		if ( request()->isPost() ) { 

			$res = model('we/We')->editData();
			if ($res>0) $this->success('更新成功','we');
			$this->error('更新失败');
		} else {
			
			$data=myEdit('we/We',$id);

			$my = new MyConfig();
			return  $my->title("编辑微信公众号")->keyId()
					    ->keyText('title', '名称')
					    ->keySelect('we_type', '类型', '', false, $this->we_type)
					    ->keyText('description', '简介')
					    ->keyImage('qrcode', '二维码')
					    ->keyText('token', 'Token')
					    ->keyText('encodingaeskey', 'EncodingAESKey')
					    ->keyText('appid', 'AppID')
					    ->keyText('appsecret', 'AppSecret')
					    ->keyText('app','应用')
					    ->keyText('rob','机器人')

					    ->group( '基本', 'fa-cog', 'id,title,we_type,desc,qrcode')
					    ->group( '微信', 'fa-wechat', 'token,encodingaeskey,appid,appsecret')
					    ->group( '机器人', 'fa-wechat', 'app,rob')
					    ->buttonSubmit()->buttonBack()
                        ->data($data)->fetch();
		}
	}


	 public function fans() {
		$my = new MyPage();
	   
	    $my ->title("微信粉丝管理")
		    ->suggest("好的生意就是用心经营好每一个顾客")
		    ->buttonNew('fansEdit')
		    
		    ->keyId()
		    ->keyText('nickname', '昵称')
		    ->keyText('remark', '备注')
		    ->keyMap('sex', '性别', ['未知','男','女'])
		    ->keyText('city', '城市')
		    ->keyText('appid', '微信')
		    ->keyMap('subscribe', '关注', ['-','ok'])
		    

		    ->actionEdit()
		    ->actionStatus('delete,disable')
		    ->actionDelete()

		    
		    ->map('nickname', '昵称')
		    ->map('wechat_type', '性别', '微信公众号类型', 'select', ['未知','男','女'])
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		return myData($my,'WeFans');
	    } else {
		    return $my->fetch();
	    }
    }


    public function fansEdit($id=0) {
		

		if ( request()->isPost() ) { 
			$res = model('we/WeFans')->editData();
			if ($res>0) $this->success('更新成功','we');
			$this->error('更新失败','we');
		} else {
			
			$data=myEdit('we/WeFans',$id);

			$my = new MyConfig();
			return  $my->title("编辑微信公众号")->keyId()
					    ->keyText('nickname', '昵称')->keyText('remark', '备注')
					    ->keySelect('sex', '性别', null, false,  $this->sex)
					    

					    ->group('basic', '基本', 'fa-cog', 'id,nickname,remark')
					    ->group('wechat', '微信', 'fa-wechat', 'sex')
					    ->buttonSubmit()->buttonBack()
                        ->data($data)->fetch();
		}
	}

	 public function menu() {
		$my = new MyPage();
	   
	    $my ->title("微信导航管理")
		    ->suggest("清晰的导航会让顾客第一眼就喜欢上你")
		    ->buttonNew('menuEdit')
		    
		    ->keyId()
		    ->keyText('appid', 'APPID')
		    ->keyLink('title', '菜单','menu')
		    ->keyText('tag_id', '性别')
		    ->keyText('sex', '性别',$this->sex)
		    ->keyText('language', '语言')
		    ->keyText('country', '祖国')
		    ->keyText('province', '省份')
		    ->keyText('city', '城市')
		    ->keyText('client_platform_type', '终端')
		    ->actionEdit()
		    ->action('menuButton?menu=####','按钮')
		    ->action('sendMenu?id=####','推送')
		    ->actionStatus('delete,disable')
		    
		    
		    ->map('title', '菜单')
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		return myData($my,'WeMenu');
	    } else {
		    return $my->fetch();
	    }
    }

    public function sendMenu($id) {
        $res=model('we/WeMenu')->sendMenu($id);
        if (!$res){
        	$this->success('同步成功');
        }else{
        	$this->error($res);
        }

	}


	// 编辑&添加
    public function menuEdit($id=0) {
		

		if ( request()->isPost() ) { 
			$res = model('we/WeMenu')->editData();
			if ($res>0) $this->success('更新成功','menu');
			$this->error('更新失败');
		} else {
			$Wes=mySelect('We','appid','title');
			$data=myEdit('we/WeMenu',$id);

			$my = new MyConfig();
			return  $my->title("编辑微信公众号")->keyId()
					    ->keyText('title', '菜单')
					    ->keySelect('appid','微信','','',$Wes)
					    ->keySelect('sex', '性别', null, false,$this->sex)
					    ->keySelect('tag_id', 'tag_id', null, false,$this->sex)
					    ->keySelect('language', 'language', null, false,$this->sex)
					    ->keySelect('country', 'country', null, false,$this->sex)
					    ->keySelect('province', 'province', null, false,$this->sex)
					    ->keySelect('city', 'city', null, false,$this->sex)
					    ->keySelect('client_platform_type', 'client_platform_type', null, false,$this->sex)
					    ->keyStatus()
					    ->buttonSubmit()->buttonBack()
                        ->data($data)->fetch();
		}
	}

	public function menuButton($menu='') {
		$my = new MyPage();
	   
	    $my ->title("按钮管理")
		    ->suggest("每一个按钮都有一个服务好顾客的心")
		    ->buttonNew('menuButtonEdit')
		    
		    ->keyId()
		    ->keyText('menu', '菜单')
		    ->keyText('name', '按钮')
		    ->keyText('pid', '父按钮')
		    ->keyText('key', '机器人',$this->sex)
		    ->keyText('sort', '排序')
		    

		    ->actionEdit()
		    ->actionStatus('delete,disable')
		    ->actionDelete()

		    
		    ->map('title', '菜单')
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		return myData($my,'WeMenuButton',['menu'=>$menu]);
	    } else {
		    return $my->fetch();
	    }
    }


	
    public function menuButtonEdit($id=0) {
		
       
		if ( request()->isPost() ) { 
			$res = model('we/WeMenuButton')->editData();
			if ($res>0) $this->success('更新成功',url('menu'));
			$this->error('更新失败');
		} else {
			$menus=mySelect('WeMenu');
			$buttons=mySelect('WeMenuButton','id','name');
			$data=myEdit('we/WeMenuButton',$id);

			$my = new MyConfig();
			return  $my->title("编辑按钮")->keyId()
					    ->keyText('name', '按钮')
					    ->keySelect('menu', '菜单', null, false,$menus)
					    ->keySelect('pid', '父按钮', null, false,['0'=>'根按钮']+$buttons)
					    ->keyText('url', '网址')
					    ->keyText('key', '机器人')
					    ->keyText('sort', '排序')
					   
					  
					    ->keyStatus()
					    ->buttonSubmit()->buttonBack()
                        ->data($data)->fetch();
		}
	}


    // 场景二维码开始
	public function scene() {
		$my = new MyPage();
	   
	    $my ->title("场景管理")
		    ->suggest("每一个场景就是一个推广渠道")
		    ->buttonNew("sceneEdit")
		    ->setDb('WeScene')
		    ->keyId()
		    ->keyText('title', '名称')
		    ->keyText('desc', '简介')
		    ->keyStatus()->keyCreateTime()->keyUpdateTime()
		    ->actionEdit()
		    ->actionStatus('delete,disable')
		    ->map('title', '名称', '微信公众号昵称')
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		return myData($my,'WeScene');
	    } else {
		    return $my->fetch();
	    }
    }


    public function sceneEdit($id=0) {
		

		if ( request()->isPost() ) { 

			$res = model('we/WeScene')->editData();
			if ($res>0) $this->success('更新成功','scene');
			$this->error('更新失败');
		} else {
			
			$data=myEdit('we/WeScene',$id);

			$my = new MyConfig();
			return  $my->title("编辑场景")->keyId()
					    ->keyText('title', '名称')
					    ->keyText('desc', '简介')
					   
					    ->buttonSubmit()->buttonBack()
                        ->data($data)->fetch();
		}
	}

	// 场景二维码开始
	public function qrcode() {
		$my = new MyPage();
	   
	    $my ->title("二维码管理")
		    ->suggest("每一个场景就是一个推广渠道")
		    ->buttonNew("qrcodeEdit",'增加临时二维码')
		    ->buttonNew(url("qrcodeEdit",['qrcode_type'=>1]),'增加永久二维码')
		    ->setDb('WeSceneQrcode')
		    ->keyId()
		    ->keyText('title', '名称')
		    ->keyText('desc', '描述')
		    ->keyJoin('shop_id', '分店', 'id', 'title', 'admin_shop')
		    ->keyJoin('scene', '场景', 'id', 'title', 'we_scene')
		    ->keyMap('qrcode_type', '类型', $this->qrcode_type)
		    ->keyText('scene_id', 'scene_id')
		    ->keyText('scene_str', 'scene_str')
		    ->keyText('short_url', '二维码')
		    ->keyText('subscribe', '关注')
		    ->keyText('SCAN', '扫码')
		    ->actionEdit()
		    ->actionStatus('delete,disable')
		    ->map('title', '名称', '微信公众号昵称')
		     ->mapStatus();

    	if ( request()->isAjax() ) {
    		return myData($my,'WeSceneQrcode');
	    } else {
		    return $my->fetch();
	    }
    }


    public function qrcodeEdit($id=0,$qrcode_type=0) {
		if ( request()->isPost() ) { 
			$res = model('We/WeSceneQrcode')->editData();
			if ($res>0) $this->success('更新成功','we');
			$this->error('更新失败','we');
		} else {

			$shops=mySelect('AdminShop');
			$scenes=mySelect('WeScene');
			$data=myEdit('we/WeSceneQrcode',$id);
			$my = new MyConfig();
			$my->title("编辑推广二维码")->keyId()
					    ->keyText('title', '二维码')
					    ->keyText('desc', '简介')
					    ->keySelect('shop_id','分店','','',$shops);
					   
					 
			if ($qrcode_type==1){
				$my->keyText('scene_str', 'scene_str','可以不填，不填就默认为scene_id')->keySelect('scene','场景','','',$scenes);
			}else{
				$my->keyText('expire', '有效期');
			}

			// $my ->keySelect('userid','类型','',$users);
			$my ->buttonSubmit()->buttonBack()
                ->data($data);
            return $my->fetch();
		}
	}

	// 场景二维码开始
	public function scan() {
		$my = new MyPage();
	   
	    $my ->title("扫码记录")
		    ->suggest("每一次扫码都会被记录")
		   
		    ->setDb('WeSceneQrcode')
		    ->keyId()
		    ->keyText('nickname', '粉丝')
		    ->keyText('appid', '微信')
		    ->keyText('qrcode', '二维码')
		    ->keyJoin('shop_id', '分店', 'id', 'title', 'admin_shop')
		    ->keyJoin('scene', '场景', 'id', 'title', 'we_scene')
		    ->keyMap('qrcode_type', '类型', $this->qrcode_type)
		    ->keyMap('do', '动作', ['扫码关注','扫码'])
		    ->keyCreateTime()
		   
		    ->actionEdit()
		    ->actionStatus('delete,disable')
		    ->map('title', '名称', '微信公众号昵称')
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		return myData($my,'WeSceneScan');
	    } else {
		    return $my->fetch();
	    }
    }


    public function scanEdit($id=0,$qrcode_type=0) {
		if ( request()->isPost() ) { 
			$res = model('We/WeSceneQrcode')->editData();
			if ($res>0) $this->success('更新成功','we');
			$this->error('更新失败','we');
		} else {

			$shops=mySelect('AdminShop');
			$scenes=mySelect('WeScene');
			$data=myEdit('we/WeSceneQrcode',$id);
			$my = new MyConfig();
			$my->title("编辑推广二维码")->keyId()
					    ->keyText('title', '二维码')
					    ->keyText('desc', '简介')
					    ->keySelect('shop_id','分店','','',$shops);
					   
					 
			if ($qrcode_type==1){
				$my->keyText('scene_str', 'scene_str','可以不填，不填就默认为scene_id')->keySelect('scene','场景','','',$scenes);
			}else{
				$my->keyText('expire', '有效期');
			}

			// $my ->keySelect('userid','类型','',$users);
			$my ->buttonSubmit()->buttonBack()
                ->data($data);
            return $my->fetch();
		}
	}





	
}